package tests.read;

import static org.junit.Assert.*;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.SublineInterface;

public class SublineQueryTest00
{
	Budget budget;
	BudgetFactory factory;
	Line line;
	Line s1;
	Line s2;

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        line = (Line) budget.createLine();
		budget.add(line);
		s1 = (Line) line.createSubline();
		s1.setName("Subline 1");
		s2 = (Line) line.createSubline();
		s2.setName("Subline 2");
		line.add(s1);
		line.add(s2);
		budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete(s1, WorkletContext.getInstance());
		Helper.delete(s2, WorkletContext.getInstance());
		Helper.delete(line, WorkletContext.getInstance());
		Helper.delete(budget, WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try{
			
			ArrayList<SublineInterface> sublines =  line.fetchSublines();
		
			if(sublines.size() != 2){
				fail("Fetch all sublines failed");
			}
			else {
				for(SublineInterface subline : sublines){
					String name = ((Line) subline).getName();
					if(!name.equals("Subline 1") || !name.equals("Subline 2")){
						fail("Fetch all sublines failed");
					}
				}
			}
		}catch(Exception e){
			fail("Fetch all sublines failed with exception " + e);
		}
	}

}

